package com.gxa.servlet.goods;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Result;
import com.gxa.pojo.goods.Type;
import com.gxa.service.goods.GoodsTypeService;
import com.gxa.service.goods.impl.GoodsTypeServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/GoodsTypeUpdateServlet")
public class GoodsTypeUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader reader = request.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        String sr;
        while ((sr=reader.readLine())!=null){
            stringBuilder.append(sr);
        }
        String s1 = stringBuilder.toString();
        Type type = JSON.parseObject(s1, Type.class);
        GoodsTypeService goodsTypeService = new GoodsTypeServiceimpl();
        int updata = goodsTypeService.updata(type);

        Map<String, Object> map = new HashMap<>();
        if(updata!=0){
            map.put("Result", Result.ok("查询成功"));
        }else {
            map.put("Result", Result.error());
        }
        String s = JSON.toJSONString(map);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
